package org.example.xsda.controller;

import org.example.xsda.common.PageResult;
import org.example.xsda.common.Result;
import org.example.xsda.service.MessageService;
import org.example.xsda.vo.MessageVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@RestController
@RequestMapping("/message")
public class MessageController {
    
    @Resource
    private MessageService messageService;
    
    @GetMapping("/page")
    public Result<PageResult<MessageVO>> getMessagePage(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String type
    ) {
        PageResult<MessageVO> pageResult = messageService.getMessagePage(current, size, type);
        return Result.success(pageResult);
    }
    
    @PostMapping("/read/{id}")
    public Result<Void> markAsRead(@PathVariable Long id) {
        messageService.markAsRead(id);
        return Result.success();
    }
    
    @PostMapping("/readAll")
    public Result<Void> markAllAsRead() {
        messageService.markAllAsRead();
        return Result.success();
    }
    
    @DeleteMapping("/{id}")
    public Result<Void> deleteMessage(@PathVariable Long id) {
        messageService.deleteMessage(id);
        return Result.success();
    }
    
    @GetMapping("/unreadCount")
    public Result<Long> getUnreadCount() {
        try {
            Long count = messageService.getUnreadCount();
            return Result.success(count);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取未读消息数失败：" + e.getMessage());
        }
    }
    
    @GetMapping("/statistics")
    public Result<Map<String, Long>> getStatistics() {
        try {
            Map<String, Long> stats = messageService.getStatistics();
            return Result.success(stats);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取消息统计失败：" + e.getMessage());
        }
    }
}

